--[[--
类名: AnnouncementView.lua
创建日期: 2016-5-31
创建人: 陈小虎
功能描述: 公告
修改内容：
修改日期:   
修改人:     
修改备注: 
]]

local BlurDialog = require("app.widget.BlurDialog")
local ModalDialog = require("app.widget.ModalDialog")
local AnnouncementView = class("AnnouncementView",function() 
    return display.newModalDialog()
end)


function AnnouncementView:ctor()
    self.mUINode=cc.CSLoader:createNode(Res.NoticeUI)
    self:addChild(self.mUINode)
    self:initView()
    display.doLayout(self.mUINode)
    self:bindListeners()
end

function AnnouncementView:initView()
    self.announcementList=self.mUINode:findChild("notice/content/notelite")
    self.announcementList:removeAllItems()

    local action = cc.CSLoader:createTimeline(Res.NoticeUI)
    self.mUINode:runAction(action)
    action:gotoFrameAndPlay(1,action:getDuration(),false)
end

function AnnouncementView:bindListeners()
    local okButton = self.mUINode:findChild("notice/Button_go")
    if okButton then
        okButton:addClickEventListener(function() 
            if self._onClickConfirmCB then
                self._onClickConfirmCB(self)
            end
        end)
    end
end

function AnnouncementView:onClickConfirm(cb)
    self._onClickConfirmCB = cb
end

function AnnouncementView:setOpenUrlDelegate(delegate)
    self._openUrlDelegate = delegate;
end

function AnnouncementView:setContent(str)
    --local str="<font size='32' color='#141414'><b/>标题1</font>"
    --str=str.."<font  size='21' color='#141414'>  <br/>因此,面对这一类文本时,解读者尽<b>可能准确</b></font>"
    --str=str.."<font>  <br/>因此,面对这一类文本时,解读者尽<font color='#ff0014'><b>可能准确</b></font></font>"
    local frameSize=self.announcementList:getContentSize()
    local contentLayout = ccui.Layout:create()
    contentLayout:setTouchSwallowEnabled(false)
    contentLayout:setTouchEnabled(false)

    local defaults={
        -- KEY_FONT_FACE=Res.font,
        KEY_FONT_SIZE=18,
        KEY_FONT_COLOR_STRING="#330202",
    }
    local richText = ccui.RichText:createWithXML(str,defaults)
    richText:ignoreContentAdaptWithSize(false)
    -- richText:setWrapMode(1)
    richText:setContentSize(cc.size(frameSize.width, 0))
    richText:formatText()
    local f_size = richText:getFormatSize()
    richText:setPosition(f_size.width*0.5,0)

    richText:setOpenUrlHandler(function(sender, url)
        if self._openUrlDelegate then
            self._openUrlDelegate(url)
        end
    end)

    contentLayout:setContentSize(cc.size(frameSize.width,f_size.height))
    contentLayout:addChild(richText)

    self.announcementList:addChild(contentLayout)

    self.announcementList:scrollToTop(0.1,true)
    self.announcementList:setBounceEnabled(true)
    self.announcementList:setGravity(ccui.ListViewGravity.centerVertical)
    self.announcementList:setClippingEnabled(true)
    self.announcementList:forceDoLayout()
    self.announcementList:refreshView()
end



return AnnouncementView